package com.lczyfz.campusinfo.modules.campusinfoexam.web;

import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.common.enums.OrderType;
import com.lczyfz.campusinfo.common.enums.PayType;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.utils.ApiResultUtils;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoTrain;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoTrainName;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoTrainNameService;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoTrainService;
import com.lczyfz.campusinfo.modules.campusinfoorder.service.CampusinfoOrderService;
import com.lczyfz.campusinfo.modules.campusinfopay.service.CampusinfoPayService;
import com.lczyfz.campusinfo.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by yt on 2017/10/24.
 * 考证培训管理 api 接口
 */
@Controller
@RequestMapping(value = "api/campusinfoexam/campusinfoTrain")
public class CampusinfoApiTrainController extends BaseController {


    @Autowired
    private CampusinfoTrainService campusinfoTrainService;
    @Autowired
    private CampusinfoTrainNameService campusinfoTrainNameService; //培训列表
    @Autowired
    private CampusinfoOrderService campusinfoOrderService;
    @Autowired
    private CampusinfoPayService campusinfoPayService;
    @RequestMapping(value = "trainList")
    @ResponseBody
    public Map<String, Object> getTrainList(CampusinfoTrainName campusinfoTrainName, HttpServletRequest request, HttpServletResponse response) {
        Page<CampusinfoTrainName> page = campusinfoTrainNameService.findPage(new Page<CampusinfoTrainName>(request, response), campusinfoTrainName);
        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        map.put("page", page.getList());
        map.put("pageNo", page.getPageNo());
        map.put("count", page.getCount());
        map.put("totalPage", page.getTotalPage());
        return map;
    }

    /**
     * 添加考证培训信息
     */
    @RequestMapping(value = "findById")//证照培训详情
    @ResponseBody
    public Map<String, Object> getexam(CampusinfoTrainName campusinfoTrainName, HttpServletRequest request, HttpServletResponse response, Model model) {
        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        CampusinfoTrainName page = campusinfoTrainNameService.get(campusinfoTrainName);
        map.put("page", page);
        return map;
    }
    @RequestMapping(value = "addTrain") //考证报名
    @ResponseBody
    public Map<String, Object> addTrain(CampusinfoTrain campusinfoTrain) {
        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        if (campusinfoTrain != null) {
            campusinfoTrain.setUser(UserUtils.getUser());
            CampusinfoTrain findcampusinfoTrain  =campusinfoTrainService.getByName(campusinfoTrain);
            if(findcampusinfoTrain==null) {
                campusinfoTrain.setOrderCode(campusinfoOrderService.saveOrderCode(OrderType.Train.getValue()));
                campusinfoTrainService.save(campusinfoTrain);
                CampusinfoTrainName campusinfoTrainName =campusinfoTrainNameService.get(campusinfoTrain.getTrainId());
                String goodName =campusinfoTrainName.getTrainName()+ OrderType.Train.getDesc();
                campusinfoPayService.ordersaveinsert(campusinfoTrain.getOrderCode(),campusinfoTrain.getPaymentMethod(),goodName,campusinfoTrainName.getPayment()); //保存订单号
                map.put("message", "报名成功");
            } else {
                findcampusinfoTrain.setPaymentMethod(PayType.UNline.getValue());
                campusinfoTrainService.save(findcampusinfoTrain);
                map.put("message","用户已经报名");
            }
        } else {
            map.put("message", "报名失败");
        }
        return map;
    }
    @RequestMapping(value = "epayaddTrain")  //考证线上支付
    @ResponseBody
    public Map<String, Object>epayaddTrain(CampusinfoTrain campusinfoTrain, HttpServletRequest request, HttpServletResponse response, Model model) {
        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        if (campusinfoTrain != null) {
            campusinfoTrain.setUser(UserUtils.getUser());
            CampusinfoTrain findcampusinfoTrain  =campusinfoTrainService.getByName(campusinfoTrain);
            if (findcampusinfoTrain==null) {
                campusinfoTrain.setOrderCode(campusinfoOrderService.saveOrderCode(OrderType.Train.getValue()));
                campusinfoTrainService.save(campusinfoTrain);
                CampusinfoTrainName  campusinfoTrainName =campusinfoTrainNameService.get(campusinfoTrain.getTrainId());
                String goodName =campusinfoTrainName.getTrainName()+ OrderType.Train.getDesc();
                campusinfoPayService.ordersaveinsert(campusinfoTrain.getOrderCode(),campusinfoTrain.getPaymentMethod(),goodName,campusinfoTrainName.getPayment()); //保存订单号
                campusinfoTrainService.save(campusinfoTrain);
                map.put("message", "报名成功");
                map.put("campusinfoTrain",  campusinfoTrain);
            } else if(findcampusinfoTrain.getStatus().equals(CampusinfoOrderStat.UNDONE.getValue())){
                map.put("campusinfoTrain",findcampusinfoTrain);
                map.put("message","用户已经报名但是未支付");
            } else {
                map.put("success",false);
                map.put("message","用户已经支付完成");
            }
        } else {
            map.put("message", "报名失败");
        }
        return  map;
    }
}
